package com.zmn.mcc.admin.controller.common;

import com.zmn.base.common.data.common.dio.company.CompanyDIO;
import com.zmn.base.common.data.common.dio.company.CompanyTypeDIO;
import com.zmn.base.common.data.common.dro.company.CompanyDRO;
import com.zmn.base.common.data.dubbo.interfaces.company.CompanyListRemoteService;
import com.zmn.base.plat.engine.common.constant.CompanyConsts;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.admin.controller.MccBaseController;
import com.zmn.mcc.model.entity.dpermit.company.BaseCompanyQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author heguanghua
 * @since 2020/05/21 17:29
 */
@Controller
@RequestMapping("common/company/")
public class CommonCompanyListController extends MccBaseController {

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    private CompanyListRemoteService companyListRemoteService;


    /**
     * 公司查询
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @RequestParam(value = "keyId", defaultValue = "0") Integer keyId,            // 主ID，用于回调方法读写相关数据
                       @RequestParam(value = "callbackUrl", defaultValue = "") String callbackUrl,  // 回调方法的url
                       @RequestParam(value = "multi", defaultValue = "1") Integer multi,            // 是否允许多选，1不允许，2允许
                       @ModelAttribute BaseCompanyQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        if (NumberUtil.isNumber(keyword)) {
            query.setCompanyId(Integer.parseInt(keyword));
            query.setName("");
        } else {
            query.setName(keyword);
        }

        query.setType(CompanyConsts.COMPANY_TYPE_SERVICE);

        CompanyDIO dio = new CompanyDIO();
        BeanUtils.copyProperties(query, dio);
        if (!CollectionUtils.isEmpty(query.getCompanyTypeQueryList())) {
            List<CompanyTypeDIO> companyTypeDIOList = query.getCompanyTypeQueryList().stream().map(e -> {
                CompanyTypeDIO dio1 = new CompanyTypeDIO();
                dio1.setType(e.getType());
                dio1.setChildType(e.getChildType());
                return dio1;
            }).collect(Collectors.toList());
            dio.setCompanyTypeQueryList(companyTypeDIOList);
        }

        List<CompanyDRO> items = new ArrayList<>();
        ResponseDTO<AMISListData<CompanyDRO>> responseDTO = companyListRemoteService.listPageCompanyDROByQuery(dio);
        if (responseDTO.isSuccess()) {
            items.addAll(responseDTO.getData().getItems());
            query.setTotalCount(companyListRemoteService.countByCompanyDIO(dio).getData());
        }

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("keyword", keyword);

        model.addAttribute("keyId", keyId);
        model.addAttribute("callbackUrl", callbackUrl);
        model.addAttribute("multi", multi);

        return "common/company/list";
    }
}
